<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'list.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#add").click(function(){
				window.location.href = "preSave.do";
			});
			
			$("#update").click(function(){
				var flag = 0;
				$("table tr td input[type=checkbox]").each(function(){
					var checkObj = $(this);
					if(checkObj.attr("checked") == "checked"){
						++flag; 
					}
				});
				if(flag != 1){
					alert("请选择一条修改");
				}else{
					var personId = "";
					$("table tr td input[type=checkbox]").each(function(){
						var checkObj = $(this);
						if(checkObj.attr("checked") == "checked"){
							personId = checkObj.val();
							return false;
						}
					});
					window.location.href = "getPerson.do?personId="+personId;
				}
				
			});
			
			$("#checkAll").change(function(){
				var checkObje = $(this);
				if(checkObje.attr("checked") == "checked"){
					$("table tr td input[type=checkbox]").each(function(){
						var checkObj = $(this);
						checkObj.attr("checked","checked");
					});
				}else{
					$("table tr td input[type=checkbox]").each(function(){
						var checkObj = $(this);
						checkObj.removeAttr("checked");
					});
				}
			});
			
			$("#delete").click(function(){
				var flag = 0;
				$("table tr td input[type=checkbox]").each(function(){
					var checkObj = $(this);
					if(checkObj.attr("checked") == "checked"){
						++flag; 
					}
				});
				if(flag < 1){
					alert("请选择至少一条删除");
				}else{
					if(confirm("真的要删吗?")){
						var ids = "";
						$("table tr td input[type=checkbox]").each(function(){
							var checkObj = $(this);
							ids = ids + checkObj.val() + ",";
						});
						ids = ids.substring(0, ids.length -1);
						window.location.href = "deletePerson.do?ids="+ids;
					}
					
				}
				
				
			});
			
		});
	</script>
  </head>
  
  <body>
  	<form action="person/getPersonByCondtion.do" method="post">
    	<table>
    		<tr>
    			<td>姓名：<input type="text" name="personName" value="${personName }"></td>
    			<td>生日：<input type="text" name="birthday" value="<fmt:formatDate value="${birthday }" pattern="yyyy-MM-dd"/>"></td>
    			<td>地址：<input type="text" name="address" value="${address }"></td>
    			<td>性别：
    				<select name="gender">
    					<option value="2" selected>--请选择--</option>
    					<option value="0" <c:if test="${gender == '0' }">selected</c:if> >男</option>
    					<option value="1" <c:if test="${gender == '1' }">selected</c:if>>女</option>
    				</select>
    			</td>
    			<td><input type="submit" value="查询" ></td>
    		</tr>
    	</table>
    </form>
    <input type="button" value="添加" id="add">
    <input type="button" value="修改" id="update">
    <input type="button" value="删除" id="delete">
    <table border="1" width="100%">
    		<tr>
    			<th><input type="checkbox" id="checkAll">全选</th>
    			<th>姓名</th>
    			<th>出生日期</th>
    			<th>家庭地址</th>
    			<th>性别</th>
    		</tr>
    		
    		<c:forEach items="${pList }" var="person">
    			<tr>
    				<td><input type="checkbox" value="${person.personId }"></td>
    				<td>${person.personName }</td>
    				<td><fmt:formatDate value="${person.birthday }" pattern="yyyy-MM-dd"/> </td>
    				<td>${person.address }</td>
    				<td>
    					<c:if test="${person.gender == 0 }">男</c:if>
    					<c:if test="${person.gender == 1 }">女</c:if>
    				</td>
    			</tr>
    		</c:forEach>
    		
    	</table>
  </body>
</html>
